package com.jiawa.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.business.domain.po.TrainCarriage;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.TrainCarriageMapper;
import com.jiawa.train.business.req.TrainCarriageQueryReq;
import com.jiawa.train.business.req.TrainCarriageSaveReq;
import com.jiawa.train.business.resp.TrainCarriageQueryResp;
import com.jiawa.train.business.service.ITrainCarriageService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 火车车厢 服务实现类
 * </p>
 *
 * @author hrz
 * @since 2025-09-07
 */
@Service
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageMapper, TrainCarriage> implements ITrainCarriageService {

    @Override
    public void saveTrainCarriage(TrainCarriageSaveReq req) {
            //计算列数和总座位数
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(colsByType.size());
        req.setSeatCount(colsByType.size() * req.getRowCount());
        //2.判断是新增还是更新
        if(ObjectUtil.isEmpty(req.getId())){
            //新增
            //1.先根据三个字段查询是否重复
            TrainCarriage one = this.lambdaQuery()
                    .eq(req.getTrainCode()!=null,TrainCarriage::getTrainCode, req.getTrainCode())
                    .eq(req.getIndex()!=null,TrainCarriage::getIndex, req.getIndex())
                    .one();
            if(ObjectUtil.isNotEmpty(one)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);
            trainCarriage.setCreateTime(LocalDateTime.now());
            trainCarriage.setUpdateTime(LocalDateTime.now());
            this.save(trainCarriage);
        }else{
            //更新
            TrainCarriage trainCarriage = this.getById(req.getId());
            BeanUtil.copyProperties(req, trainCarriage);
            trainCarriage.setUpdateTime(LocalDateTime.now());
            this.updateById(trainCarriage);
        }
    }

    @Override
    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req) {
        //1.取得车次编号
        String trainCode = req.getTrainCode();
        //2分页查询
        Page<TrainCarriage> page = this.lambdaQuery()
                .eq(TrainCarriage::getTrainCode, trainCode)
                .orderByDesc(TrainCarriage::getIndex)
                .page(new Page<>(req.getPage(), req.getSize()));
        List<TrainCarriage> records = page.getRecords();
        if(ObjectUtil.isEmpty(records)){
            PageResp<TrainCarriageQueryResp> objectPageResp = new PageResp<>();
            objectPageResp.setTotal(0L);
            objectPageResp.setList(new ArrayList<>());
            return objectPageResp;
        }
        //3.转VO返回
        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(records, TrainCarriageQueryResp.class);
        PageResp<TrainCarriageQueryResp> objectPageResp = new PageResp<>();
        objectPageResp.setTotal(page.getTotal());
        objectPageResp.setList(list);
        return objectPageResp;
    }

    @Override
    public void delete(Long id) {
        if(ObjectUtil.isEmpty(this.getById(id))){
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        }
        this.removeById(id);
    }
}
